package com.daikuan.fffkuaiban.adapter;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.daikuan.fffkuaiban.R;
import com.daikuan.fffkuaiban.bmobBean.ListConfig;
import com.daikuan.fffkuaiban.utils.ImageLoader;
import com.jarhead.common.commonutils.DensityUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by jiming1 on 2017/7/1.
 */
public class HomeListAdapter2 extends BaseAdapter {
	private Context context;
	private List<ListConfig> listConfigs;

	public HomeListAdapter2(Context context, List<ListConfig> listConfigs) {
		this.context = context;
		this.listConfigs = listConfigs;
	}

	@Override
	public int getCount() {
		return listConfigs.size();
	}

	@Override
	public ListConfig getItem(int i) {
		return listConfigs.get(i);
	}

	@Override
	public long getItemId(int i) {
		return 0;
	}

	@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
	@Override
	public View getView(int i, View view, ViewGroup viewGroup) {
		BaseViewHolder viewHolder = null;
		final ListConfig hotChannel = getItem(i);
		if (view == null) {
			view = LayoutInflater.from(context).inflate(R.layout.item_home2, viewGroup, false);
			viewHolder = new BaseViewHolder(view);
			view.setTag(viewHolder);
		} else {
			viewHolder = (BaseViewHolder) view.getTag();
		}

		viewHolder.tvName.setText(hotChannel.channel_name);
		viewHolder.tvQuotaRange.setText(hotChannel.money_range);
		viewHolder.tvInfo.setText(hotChannel.channel_desp);
		viewHolder.llContain.removeAllViews();
		if (!TextUtils.isEmpty(hotChannel.tags)) {
			String[] tags = hotChannel.tags.split("，");
			if (tags != null && tags.length > 0) {

				for (int i1 = 0; i1 < tags.length; i1++) {
					String tag = tags[i1];
//                    String tag ="身份证";
					TextView textView = new TextView(context);
					textView.setText(tag);
					textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10f);
					textView.setTextColor(Color.WHITE);
					textView.setBackground(ContextCompat.getDrawable(context, R.drawable.label_drawable));
					LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
					int margin = DensityUtil.dip2px(context, 3);
					params.setMargins(margin, 0, margin, 0);
					textView.setLayoutParams(params);
					int padding = DensityUtil.dip2px(context, 5);
					textView.setPadding(padding, margin, padding, margin);
					textView.setSingleLine();
					if (i1 < 3) {
						viewHolder.llContain.addView(textView);
					}
				}
			}
		}
		if (hotChannel.channel_logo == null) {
			ImageLoader.loadImage(context, "", viewHolder.ivImage);
		} else {
			ImageLoader.loadImage(context, hotChannel.channel_logo.url, viewHolder.ivImage);
		}


		return view;
	}

	public void update(List<ListConfig> beans) {
		this.listConfigs.clear();
		this.listConfigs.addAll(beans);
		notifyDataSetChanged();
	}

	public void add(List<ListConfig> beans) {
		this.listConfigs.addAll(beans);
		notifyDataSetChanged();
	}

	class BaseViewHolder {
		@Bind(R.id.ivImage)
		ImageView ivImage;
		@Bind(R.id.tvName)
		TextView tvName;
		@Bind(R.id.tvInfo)
		TextView tvInfo;
		@Bind(R.id.tvQuotaRange)
		TextView tvQuotaRange;
		@Bind(R.id.llContain)
		LinearLayout llContain;

		public BaseViewHolder(View view) {
			ButterKnife.bind(this, view);
		}
	}


}
